home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / vol7n9.arc / DUMP.ASM next >
Assembly Source File  |  1988-04-08  |  11KB  |  260 lines

  1. ;-----------------------------------------------
  2. ; DUMP.ASM -- OS/2 Hex Dump Program
  3. ;             (c) 1988, Ziff Communications Co.
  4. ;-----------------------------------------------
  5.  
  6.                EXTRN  DosOpen:FAR,  DosRead:FAR, DosWrite:FAR
  7.                EXTRN  DosClose:FAR, DosExit:FAR
  8.  
  9.                DOSSEG
  10.                .286
  11.                .MODEL SMALL
  12.                .STACK 200h
  13.                                         ;--------------------------
  14.                .DATA                    ; Initialized Data Segment
  15.                                         ;--------------------------
  16.  
  17. SyntaxMsg      db   13, 10,     "Syntax: DUMP filename"
  18.                db   13, 10, 10, "DUMP (c) 1988, Ziff Communications Co."
  19.                db   13, 10,     "PC Magazine ", 254," Charles Petzold - 12/87"
  20.                db   13, 10
  21. SyntaxMsgLen   equ  $ - SyntaxMsg
  22.  
  23. FileSpecMsg    db   "DUMP: File not found or cannot be opened"
  24. FileSpecMsgLen equ  $ - FileSpecMsg
  25.  
  26. Delimiters     db   9, " ,;=", 0
  27. Address        dw   0, 0
  28.                                         ;----------------------------
  29.                 .DATA?                  ; Uninitialized Data Segment
  30.                                         ;----------------------------
  31. FileHandle     dw   ?
  32. OpenAction     dw   ?
  33. BytesRead      dw   ?
  34. BytesWritten   dw   ?
  35. InputBuffer    db   16 dup (?)
  36. OutputBuffer   db   80 dup (?)
  37.                                         ;--------------
  38.                 .CODE                   ; Code Segment
  39.                                         ;--------------
  40.  
  41. ;----------------------------------------------
  42. ; Parse command line to get file specification
  43. ;----------------------------------------------
  44.  
  45. Entry:         Push DS                       ; Data segment selector
  46.                Pop  ES                       ; Transfer it to ES
  47.                Mov  DS, AX                   ; DS = Environment selector
  48.                Mov  SI, BX                   ; SI = Start of command line
  49.  
  50. SkipProgName:  Lodsb                         ; Pull a command line byte
  51.                Or   AL, AL                   ; Check if it's zero
  52.                Jnz  SkipProgName             ; If not, continue
  53.  
  54. SkipDelims:    Lodsb                         ; Get byte from parameter
  55.                Or   AL, AL                   ; Check if it's zero
  56.                Jz   DisplaySyntax            ; If so, exit with message
  57.  
  58.                Mov  DI, Offset Delimiters    ; Check if delimiter
  59.                Mov  CX, 5                    ; There are 5 of them
  60.                Repne Scasb                   ; Scan the string
  61.                Jz   SkipDelims               ; If delimiter, loop back
  62.  
  63.                Mov  DX, SI                   ; Pointer to file name + 1
  64.                Dec  DX                       ; Pointer to file name
  65.  
  66. SearchFileEnd: Lodsb                         ; Get a byte
  67.                Mov  DI, Offset Delimiters    ; Check if delimiter
  68.                Mov  CX, 6                    ; 6 of them now including 0
  69.                Repne Scasb                   ; Scan the string
  70.                Jnz  SearchFileEnd            ; If not delimiter, do it again
  71.  
  72.                Mov  Byte Ptr [SI - 1], 0     ; Terminate with zero byte
  73.                Jmp  Short OpenFile
  74.  
  75. ;------------
  76. ; Error Exit
  77. ;------------
  78.  
  79. DisplaySyntax: Push ES                       ; Otherwise, set DS to
  80.                Pop  DS                       ;   data segment
  81.                Mov  DX, Offset SyntaxMsg     ; Syntax message offset
  82.                Mov  CX, SyntaxMsgLen         ; Syntax message length
  83.  
  84. ErrorExit:     Push 2                        ; Standard error handle
  85.                Push DS                       ; Segment of string
  86.                Push DX                       ; Offset of string
  87.                Push CX                       ; Length of string
  88.                Push DS                       ; Segment for bytes written
  89.                Push Offset BytesWritten      ; Offset for bytes written
  90.                Call DosWrite                 ; Display the message
  91.  
  92.                Push 1                        ; Terminte all threads
  93.                Push 1                        ; Return code
  94.                Call DosExit                  ; Exit
  95.  
  96. ;-------------------------
  97. ; Open file using DosOpen
  98. ;-------------------------
  99.  
  100. OpenFile:      Push DS                       ; Exchange DS and ES
  101.                Push ES
  102.                Pop  DS
  103.                Pop  ES
  104.  
  105.                Push ES                       ; Segment of file name
  106.                Push DX                       ; Offset of file name
  107.                Push DS                       ; Segment for file handle
  108.                Push Offset FileHandle        ; Offset for file handle
  109.                Push DS                       ; Segment for action
  110.                Push Offset OpenAction        ; Offset for action
  111.                Push 0                        ; High size (not used)
  112.                Push 0                        ; Low size (not used)
  113.                Push 0                        ; Attribute (not used)
  114.                Push 1                        ; Open flag (open if file exists)
  115.                Push 20h                      ; Open mode (read, deny write)
  116.                Push 0                        ; Reserved
  117.                Push 0                        ; Reserved
  118.                Call DosOpen                  ; Open the file
  119.  
  120.                Or   AX, AX                   ; Check for error
  121.                Jz   ReadAndDump              ; If none, continue
  122.  
  123.                Mov  DX, Offset FileSpecMsg   ; Error message
  124.                Mov  CX, FileSpecMsgLen       ; Length of message
  125.  
  126.                Jmp  ErrorExit                ; Print it and exit
  127.  
  128. ;---------------------------------------------
  129. ; Read 16 bytes from file and display address
  130. ;---------------------------------------------
  131.  
  132. ReadAndDump:   Push DS                       ; Set ES to DS
  133.                Pop  ES
  134.  
  135. MainLoop:      Push [FileHandle]             ; File Handle
  136.                Push DS                       ; Segment of buffer
  137.                Push Offset InputBuffer       ; Offset of buffer
  138.                Push 16                       ; Length of buffer
  139.                Push DS                       ; Segment for bytes read
  140.                Push Offset BytesRead         ; Offset for bytes read
  141.                Call DosRead                  ; Read the file
  142.  
  143.                Cmp  [BytesRead], 0           ; See if no bytes read
  144.                Jnz  DoLine                   ; If bytes read, continue
  145.  
  146.                Jmp  AllDone                  ; If zero bytes read, end program
  147.  
  148. DoLine:        Mov  DI, Offset OutputBuffer  ; Destination
  149.  
  150.                Mov  AX, [Address + 2]        ; High address within file
  151.                Call HexWordOut               ; Display the value
  152.                Mov  AL, '-'                  ; A dash
  153.                Stosb
  154.                Mov  AX, [Address]            ; Low offset within file
  155.                Call HexWordOut               ; Display it
  156.  
  157.                Mov  AL, ' '                  ; Follow it with a blank
  158.                Stosb
  159.  
  160. ;---------------------
  161. ; Prepare Hex Display
  162. ;---------------------
  163.  
  164.                Mov  SI, Offset InputBuffer   ; Source
  165.                Mov  CX, [BytesRead]          ; Number of bytes read
  166.                Sub  BX, BX                   ; Initialize counter
  167.  
  168. HexLoop:       Mov  AL, ' '                  ; A blank normally precedes
  169.                Cmp  BX, 8                    ; But check if counter is 8
  170.                Jnz  DoTheSpace               ; If not, a blank is OK
  171.  
  172.                Mov  AL, '-'                  ; Otherwise, use a dash
  173. DoTheSpace:    Stosb                         ; Display it
  174.  
  175.                Lodsb                         ; Get a byte
  176.                Call HexByteOut               ; Convert it to hex
  177.  
  178.                Inc  BX                       ; Kick up the counter
  179.                Loop HexLoop                  ; And loop for next byte
  180.  
  181.                Mov  CX, 16                   ; 16 possible bytes
  182.                Sub  CX, BX                   ; Subtract those already done
  183.                IMul CX, 3                    ; Multiply by 3
  184.                Add  CX, 2                    ; And add 2 for the end
  185.                Mov  AL, ' '                  ; This is all blank
  186.                Rep  Stosb                    ; Store the blanks
  187.  
  188. ;---------------------------
  189. ; Prepare Character Display
  190. ;---------------------------
  191.  
  192.                Mov  SI, Offset InputBuffer   ; Source
  193.                Mov  CX, [BytesRead]          ; Number of bytes
  194.  
  195. CharLoop:      Lodsb                         ; Get the character
  196.                Cmp  AL, ' '                  ; See if displayable ASCII
  197.                Jb   DisplayPeriod            ; If below space, use dot
  198.  
  199.                Cmp  AL, 7Fh                  ; If DEL or above, use dot
  200.                Jb   DisplayChar
  201.  
  202. DisplayPeriod: Mov  AL, '.'                  ; Display period
  203.  
  204. DisplayChar:   Stosb                         ; Display character
  205.                Loop CharLoop                 ; Do next character
  206.  
  207.                Mov  AL, 13                   ; Carriage return
  208.                Stosb
  209.                Mov  AL, 10                   ; And line feed
  210.                Stosb
  211.  
  212. ;-----------------------------
  213. ; Display Line using DosWrite
  214. ;-----------------------------
  215.  
  216.                Sub  DI, Offset OutputBuffer  ; Length of output line
  217.  
  218.                Push 1                        ; File Handle: Standard Output
  219.                Push DS                       ; Segment of buffer
  220.                Push Offset OutputBuffer      ; Offset of buffer
  221.                Push DI                       ; Length of buffer
  222.                Push DS                       ; Segment for bytes written
  223.                Push Offset BytesWritten      ; Offset for bytes written
  224.                Call DosWrite                 ; Display it
  225.  
  226.                Add  [Address], 16            ; Kick up address
  227.                Adc  [Address + 2], 0
  228.                Jmp  MainLoop                 ; Do another line
  229.  
  230. AllDone:       Push [FileHandle]             ; File handle
  231.                Call DosClose                 ; Close the file
  232.  
  233.                Push 1                        ; Exit program
  234.                Push 0
  235.                Call DosExit
  236.  
  237. ;----------------------------------
  238. ; Subroutines to store line output
  239. ;----------------------------------
  240.  
  241. HexWordOut:    Xchg AL, AH
  242.                Call HexByteOut               ; Do high byte first
  243.                Xchg AL, AH
  244.                                              ; Fall through
  245. HexByteOut:    Push AX
  246.                Shr  AL, 4
  247.                Call HexDigitOut              ; Do high digit first
  248.                Pop  AX
  249.                And  AL, 0Fh
  250.                                              ; Fall through
  251. HexDigitOut:   Add  AL, 90h
  252.                Daa                           ; Common trick to convert
  253.                Adc  AL, 40h                  ;   hex digit to ASCII
  254.                Daa
  255.  
  256.                Stosb                         ; Store in line buffer
  257.                Ret
  258.  
  259.                END  Entry
  260.